import 'dart:io';

import 'package:get/get.dart';
import 'package:kkhousekeep/models/image_upload_entity.dart';
import 'package:kkhousekeep/net/base_connect.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import 'api.dart';

class UploadImageConnect extends BaseConnect {
  Future<ImageUploadEntity?> uploadImage(String imagePath) async {
    File imageFile = File(imagePath);
    String fileName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
    MultipartFile multipartFile = MultipartFile(
      imageFile,
      filename: fileName,
    );
    FormData formData = FormData({"file": multipartFile});
    var response = await post(Api.uploadImage, formData,
        contentType: "multipart/form-data");
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      ImageUploadEntity.fromJson(response.body),
    );
  }
}
